﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class EnemyControl : MonoBehaviour
{
    //1
    public Transform hppos;
    public float speet = 5;
    [HideInInspector]
    public float Attackdic = 4.5f;
    float attacktime = 2f;
    GameObject panel;
    GameObject hpboj;
    int Hp;
    int Hp2;
    //2
    bool ismove = true;
    //12
    CharacterController cc;
    GameObject player;
    private void Awake()
    {
        VariableDocument.GetInstance.anima = this.GetComponent<Animator>();
        cc = this.GetComponent<CharacterController>();
        player = GameObject.FindGameObjectWithTag("Player");
        panel = GameObject.FindGameObjectWithTag("panel");
        if (SceneManager.GetActiveScene().name == "GameScene")
        {
            HpInit();
        }
    }

    private void HpInit()
    {
        GameObject game = DataManager.GetInstance.Loadasset(ResourcesEnum.Prefab, "hpbg");
        GameObject temp = Instantiate(game, panel.transform, false);
        VariableDocument.GetInstance.uiobj = temp.GetComponent<UIFixedObj>();
        VariableDocument.GetInstance.uiobj.Init(hppos.gameObject, DataManager.GetInstance.enemdic[VariableDocument.GetInstance.through].name);
    }

    public void HpValue(int hp)
    {
        Hp = hp;
        Hp2 = hp;
    }
      

    private void Update()
    {
        if(SceneManager.GetActiveScene().name == "GameScene")
        {
            if (Vector3.Distance(this.transform.position, player.transform.position) < 5f)
                Const.isjx = true;
            else
                Const.isjx = false;
            if (Vector3.Distance(this.transform.position, player.transform.position) < Attackdic)
            {
                if (Const.isgameover)
                    return;
                this.transform.LookAt(player.transform);
                MoveAnima(false);
                attacktime += Time.deltaTime;
                if (attacktime >= 1.5f)
                {
                    AttackAnima();
                    attacktime = 0;
                }
                Vector3 dir = player.transform.position - transform.position;
                float num = Vector3.Dot(transform.forward, dir);
            }
            else
            {
                this.transform.LookAt(player.transform);
                cc.SimpleMove(this.transform.forward * speet);
                MoveAnima(true);
                attacktime = 1.5f;
            }
        }
        if(SceneManager.GetActiveScene().name == "BattleScene")
        {
            if (Const.isgameover)
            {
                DieAnima();
                return;
            }
            else
            {
                if (!ismove)
                    return;
                MoveAnima(true);
                cc.SimpleMove(this.transform.forward * 10f);
            }
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (SceneManager.GetActiveScene().name == "BattleScene")
        {
            if (other.name == "PlayEvent")
            {
                ismove = false;
                Enemydie();
                GameControl._Instance.PlayerAttack();
            }

            if (other.name == "Dieobj")
            {
                Destroy(this.gameObject);
            }
        }
            
    }
    private void Enemydie()
    {
        //animator.SetTrigger("isdie");
        HpInit2();
        StartCoroutine(Destroyobj());
    }
    private void HpInit2()
    {
        player.transform.GetChild(4).gameObject.SetActive(true);
    }

    IEnumerator Destroyobj()
    {
        yield return new WaitForSeconds(0.5f);
        player.transform.GetChild(4).gameObject.SetActive(false);
        //每场游戏都要掉0-3个装备
        int m = RandomNum.GetRandomNum(1, 10);
        if (VariableDocument.GetInstance.dropped_equipment <= 3)
        {
            //掉落装备概率为五分之一
            if (m % 5 == 0)
            {
                int n = RandomNum.GetRandomNum(0, 18);
                VariableDocument.GetInstance.dropped_equipment_id[VariableDocument.GetInstance.dropped_equipment] = n;
                VariableDocument.GetInstance.dropped_equipment += 1;
                BattleScenelui._instance.dropEquipment.GetComponent<SpriteRenderer>().sprite = GetNewItem._instance.item[n].itemImage;
                BattleScenelui._instance.dropEquipment.SetActive(true);
            }
        }
        //掉落元宝的概率为十分之三
        if (m % 3 == 0)
        {
            VariableDocument.GetInstance.dropped_equipment_id[3] += 1;
            BattleScenelui._instance.dropMoney.SetActive(true);
        }
        Destroy(this.gameObject);
    }


    public void EnemyJx()
    {
        CalculateHp();
    }

  

    void CalculateHp()
    {
        int num = Random.Range(30*5, 50*5);
        ShowHp(num);
        Hp -= num;
        //生成减血显示
        if (Hp <= 0)
        {
            PlayerControl.boosNotDied = false;
            DieAnima();
            VariableDocument.GetInstance.uiobj.JXue(0);
            GameUiControl._instance.VictoryClick();
        }
        else
        {
            EnemyHit();
            VariableDocument.GetInstance.uiobj.JXue((float)Hp / Hp2);
        }
    }


    void ShowHp(int hpvalue)
    {
        VariableDocument.GetInstance.uiobj.Createhp(hpvalue);
    }

    public void MoveAnima(bool ismove)
    {
        VariableDocument.GetInstance.anima.SetBool("ismove", ismove);   
    }

    public void EnemyHit()
    {

        if (VariableDocument.GetInstance.anima == null)
            return;
        //GameAllData.GetInstance.anima.SetTrigger("ishit");
    }


    public void DieAnima()
    {
        VariableDocument.GetInstance.anima.SetTrigger("isdie"); 
    }

    public void AttackAnima()
    {
        VariableDocument.GetInstance.anima.SetTrigger("isattack");
    }


    public void AttackHandle()
    {
        GameUiControl._instance.PlayerJianxue();
    }


    public void DieHandle()
    {
        VariableDocument.GetInstance.uiobj.gameObject.SetActive(false);
        Destroy(VariableDocument.GetInstance.uiobj);
        Destroy(this.gameObject);
    }

}
